//
//  XMFindViewController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/7.
//

#import "XMFindViewController.h"
#import "XMFindView.h"
#import "XMNetwork+find.h"
#import "XMLoginOtherPhoneController.h"
#import "XMPicwordModel.h"
#import "XMPicwordPublishViewController.h"
#import "XMPicwordDetailViewController.h"
#import "XMPicwordSearchViewController.h"

@interface XMFindViewController ()<XMFindViewDelegate>

@property (nonatomic, strong) XMFindView *findView;

@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *pathStr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation XMFindViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hideNavigationBar:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    _page = 1;
    [self.view addSubview:self.findView];
    [self getPicwordListWithPath:getRecommendUGCNologin city:@""];
}

- (void)getPicwordListWithPath:(NSString *)pathStr
                          city:(NSString *)city {
    _pathStr = pathStr;
    XMWeakSelf;
    [_Network getPicWordListWithPath:pathStr
                             pageNum:[NSString stringWithFormat:@"%ld", _page]
                           sessionId:@""
                                city:city
                             success:^(id  _Nonnull responseObject) {
        NSArray *modelArr = [NSArray yy_modelArrayWithClass:[XMPicwordModel class] json:[responseObject safeArrayForKey:@"data"]];
        [weakSelf.findView setContentWithPicwordModelArr:modelArr
                                                    page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
- (void)tabSelect:(NSInteger)selectIndex {
    _page = 1;
    switch (selectIndex) {
        case 0: {
            [self getPicwordListWithPath:getRecommendUGCNologin city:@""];
        }
            break;
        case 1: {
            XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
            if (userModel && userModel.refreshToken.length > 0) {
                [self getPicwordListWithPath:getFindAttentionUrl city:@""];
            }
            else {
                XMBaseViewController *currentVC = (XMBaseViewController *)[AppDelegate getCurrentVC];
                if (![currentVC isKindOfClass:[XMLoginOtherPhoneController class]]) {
                    XMLoginOtherPhoneController *loginVC = [[XMLoginOtherPhoneController alloc] init];
                    UINavigationController *loginNVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
                    loginNVC.modalPresentationStyle = UIModalPresentationFullScreen;
                    [currentVC presentViewController:loginNVC animated:YES completion:nil];
                }
            }
        }
            break;
        case 2: {
            [self getPicwordListWithPath:getCurrentCityListUrl city:_city];
        }
            break;
            
        default:
            break;
    }
}

//发布
- (void)picWordPublishAction {
    XMPicwordPublishViewController *publishVC = [[XMPicwordPublishViewController alloc] init];
    publishVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:publishVC animated:YES];
}

//搜索
- (void)picWordSearchAction {
    XMPicwordSearchViewController *searchVC = [[XMPicwordSearchViewController alloc] init];
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)picwordLikedWithPicwordId:(NSString *)picwordId {
    XMWeakSelf;
    [_Network videoLikedWithVideoId:picwordId
                            success:^(NSError * _Nonnull error) {
        [weakSelf refreshData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)refreshData {
    _page = 1;
    NSString *city = @"";
    if ([_pathStr isEqualToString:getCurrentCityListUrl]) {
        city = _city;
    }
    [self getPicwordListWithPath:_pathStr city:city];
}

- (void)loadMore {
    _page += 1;
    NSString *city = @"";
    if ([_pathStr isEqualToString:getCurrentCityListUrl]) {
        city = _city;
    }
    [self getPicwordListWithPath:_pathStr city:city];
}

- (void)chatPicwordLikedWithPicwordId:(NSString *)picwordId {
    XMPicwordDetailViewController *detailVC = [[XMPicwordDetailViewController alloc] init];
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.picwordId = picwordId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (XMFindView *)findView {
    if (!_findView) {
        _findView = [[XMFindView alloc] initWithFrame:self.view.bounds];
        _findView.delegate = self;
    }
    return _findView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
